package org.jeecg.modules.middle.auditCenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.LimitSubmit;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.manage.entity.CaishuiBill;
import org.jeecg.modules.middle.auditCenter.service.ICaishuiBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Description: 开票记录
 * @Author: yfkj
 * @Date: 2023-02-23
 * @Version: V1.0
 */
@Api(tags = "开票记录[中台]")
@RestController
@RequestMapping("/auditCenter/caishuiBill")
@Slf4j
public class CaishuiBillController extends JeecgController<CaishuiBill, ICaishuiBillService> {

    @Autowired
    private ICaishuiBillService caishuiBillService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 分页列表查询
     *
     * @param caishuiBill
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "开票记录[中台]-分页列表查询", notes = "开票记录[中台]-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CaishuiBill>> queryPageList(CaishuiBill caishuiBill,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<CaishuiBill> queryWrapper = QueryGenerator.initQueryWrapper(caishuiBill, req.getParameterMap());
        Page<CaishuiBill> page = new Page<CaishuiBill>(pageNo, pageSize);
        IPage<CaishuiBill> pageList = caishuiBillService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 开票审核[中台]
     * @param id
     * @param status
     * @return
     */
    @AutoLog(value = "开票记录[中台]-开票审核", terminal = 1)
    @ApiOperation(value="开票记录[中台]-开票审核", notes="开票记录[中台]-开票审核")
    @GetMapping(value = "/statusOne")
    @LimitSubmit(key = "middle:bill:statusOne:%tId", spelKey = "#id")
    public Result<?> statusOne(@RequestParam(name="id",required=true) String id,
                               @RequestParam(name="captcha",required=true) String captcha,
                               @RequestParam(name="rejectReason",required=false) String rejectReason,
                               @RequestParam(name="status",required=true) int status) {
        //从缓存中获取手机验证码
        Object code = redisUtil.get(CommonConstant.PHONE_REDIS_KEY_PRE + TokenUtils.getLoginUser().getUsername());
        if(oConvertUtils.isEmpty(code)){
            return Result.error("短信验证码已失效！");
        }else if (!captcha.equals(code)) {
            return Result.error("短信验证码错误！");
        }

        CaishuiBill caishuiBill = caishuiBillService.getById(id);
        if(oConvertUtils.isEmpty(caishuiBill)){
            return Result.error("开票信息错误！");
        }else if(caishuiBill.getStatus() != 1){
            return Result.error("审核失败！");
        }

        caishuiBill.setStatus(status);
        caishuiBill.setCheckTime(new Date());
        caishuiBill.setRejectReason(rejectReason);
        caishuiBillService.updateById(caishuiBill);
        return Result.OK("审核成功!");
    }

    /**
     *  上传票据
     *
     * @param caishuiBill
     * @return
     */
    @AutoLog(value = "开票记录[中台]-上传票据", terminal = 1)
    @ApiOperation(value="开票记录[中台]-上传票据", notes="开票记录[中台]-上传票据")
    @PostMapping(value = "/updateBill")
    @LimitSubmit(key = "middle:bill:updateBill:%tId", spelKey = "#caishuiBill.id")
    public Result<String> updateBill(@RequestBody CaishuiBill caishuiBill) {
        if(caishuiBill.getReceivedType()==1 && oConvertUtils.isEmpty(caishuiBill.getElectronicInvoice())){
            return Result.error("请上传电子发票！");
        }
        if(caishuiBill.getReceivedType()==2 && oConvertUtils.isEmpty(caishuiBill.getBillImg())){
            return Result.error("请上传纸质票据！");
        }
        caishuiBill.setFinishTime(new Date());
        caishuiBill.setStatus(7);
        caishuiBillService.updateById(caishuiBill);
        return Result.OK("上传成功！");
    }

}
